/**
 *  在线支付模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	
	require("../../custom/base/component-core.js");
		
	

	
	var validate = require("../../custom/base/component-validate.js");
	
	
	var async = require("../../custom/base/component-async.js");
	
	
		
	//初始化函数
	exports.init=function(m){
		
		window.$ldh_pay=m;
	
		
	};
	
   exports.goWxPay=function(code,url){
		
		
		var params = {
				"code":code,
				"url":url,
				"tradeType":"JSAPI",
				"userId":"201406262325401643",
				"orderAmount":"0.01",
				"orderId":"201504221137204819",
				"channelCode":"01"
		};
		
		eval(
					Wind.compile("async", function() {
						
						var data = $await(async.DDF_AsyncRequest("POST", ctx+"/shopapp/WebPay/payByWXPay.do", params));
						//alert(data.code+"***"+data.appId+"***"+data.paySign+"***"+data.signType+"***"+data.nonceStr);
						if(data.code=="1"){
							

							 // 加载一个模块
							  require.async(['http://res.wx.qq.com/open/js/jweixin-1.0.0.js','http://www.jujie.xyz/mall/sha1.js'], function(wx,b) {
								  
								  //var url=location.href.split('#')[0];
								  //alert(CryptoJS.SHA1("jsapi_ticket="+data["ticket"]+"&noncestr="+data["noncestr"]+"&timestamp="+data["timestamp"]+"&url="+url));
								 /* $("#appid").val(data["appid"]);
								  $("#timestamp").val(data["timestamp"]);
								  $("#noncestr").val(data["noncestr"]);
								  $("#url").val(wx_pay_url);
								  $("#signature").val(CryptoJS.SHA1("jsapi_ticket="+data["ticket"]+"&noncestr="+data["noncestr"]+"&timestamp="+data["timestamp"]+"&url="+wx_pay_url));
								  $("#signature_server").val(data["signature"]);*/

							    wx.config({
								      debug: false,
								      appId: data["appid"],
								      timestamp: data["timestamp"],
								      nonceStr: data["noncestr"],
								      signature:data["config_signature"],
								      jsApiList: [
								                  'checkJsApi',
								                  'onMenuShareTimeline',
								                  'onMenuShareAppMessage',
								                  'onMenuShareQQ',
								                  'onMenuShareWeibo',
								                  'chooseWXPay'
								      ]
								  });
								
								  
								   wx.ready(function () {
									   
									 var packages=data["package"];
									   wx.chooseWXPay({
									       timestamp: data["timestamp"],
									       nonceStr: data["noncestr"],
									       package:packages,
									       signType: "MD5", // 注意：新版支付接口使用 MD5 加密
									       paySign:data["sign"],
									       success: function (res) {
									           // 支付成功后的回调函数
									    	   alert("支付成功");
									       }
									     });
								  });  
								  
								   wx.error(function (res) {
								 	  
								 	 if(res.errMsg=="chooseWXPay:cancel"){
								 		 
								 		alert("你已取消支付");
								 		
								 	 }else{
								 		 
								 		 
								 		 alert(res.errMsg);
								 	 }
								 	
								 	});
							  });
							
							

						}else{
							
						}
						
				
		}))().start();
		
	};
	
});

